package com.lht.cozyCloud.bee.controller;

import com.lht.cozyCloud.bee.domain.MessageTemplate;
import com.lht.cozyCloud.bee.mapper.MessageRecordMapper;
import com.lht.cozyCloud.bee.service.MessageTemplateService;
import com.lht.cozyCloud.common.core.utils.poi.ExcelUtil;
import com.lht.cozyCloud.common.core.web.controller.BaseController;
import com.lht.cozyCloud.common.core.web.domain.AjaxResult;
import com.lht.cozyCloud.common.core.web.page.TableDataInfo;
import com.lht.cozyCloud.common.log.annotation.Log;
import com.lht.cozyCloud.common.log.enums.BusinessType;
import com.lht.cozyCloud.common.security.annotation.RequiresPermissions;
import com.lht.cozyCloud.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author lht
 * @version 1.0
 * @description: TODO
 * @date 2025/1/3 9:44
 */

@RestController
@RequestMapping("/template")
public class MessageTemplateController extends BaseController {

    @Autowired
    private MessageTemplateService messageTemplateService;

    @RequiresPermissions("bee:template:list")
    @GetMapping("/list")
    public TableDataInfo list(MessageTemplate template)
    {
        startPage();
        List<MessageTemplate> list = messageTemplateService.selectTemplateList(template);
        return getDataTable(list);
    }

    @Log(title = "模板管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("bee:template:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, MessageTemplate template)
    {
        List<MessageTemplate> list = messageTemplateService.selectTemplateList(template);
        ExcelUtil<MessageTemplate> util = new ExcelUtil<>(MessageTemplate.class);
        util.exportExcel(response, list, "参数数据");
    }

    /**
     * 根据参数编号获取详细信息
     */
    @GetMapping(value = "/{templateId}")
    public AjaxResult getInfo(@PathVariable Long templateId)
    {
        return success(messageTemplateService.selectTemplateById(templateId));
    }

    /**
     * 根据参数键名查询参数值
     */
    @GetMapping(value = "/template/{type}")
    public AjaxResult getTemplateKey(@PathVariable String type)
    {
        return success(messageTemplateService.selectTemplateByType(type));
    }

    /**
     * 新增参数配置
     */
    @RequiresPermissions("bee:template:add")
    @Log(title = "模板管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody MessageTemplate template)
    {
        if (!messageTemplateService.checkTemplateTypeUnique(template))
        {
            return error("新增模板类型'" + template.getType() + "'失败，该类型已存在");
        }
        template.setCreateBy(SecurityUtils.getUsername());
        return toAjax(messageTemplateService.insertTemplate(template));
    }

    /**
     * 修改参数配置
     */
    @RequiresPermissions("bee:template:edit")
    @Log(title = "模板管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody MessageTemplate template)
    {
        if (!messageTemplateService.checkTemplateTypeUnique(template))
        {
            return error("修改模板类型'" + template.getType() + "'失败，模板类型已存在");
        }
        template.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(messageTemplateService.updateTemplate(template));
    }

    /**
     * 删除参数配置
     */
    @RequiresPermissions("bee:template:remove")
    @Log(title = "模板管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{templateId}")
    public AjaxResult remove(@PathVariable Long[] templateId)
    {
        messageTemplateService.deleteTemplateByIds(templateId);
        return success();
    }

    /**
     * 刷新参数缓存
     */
    @RequiresPermissions("bee:template:refreshCache")
    @Log(title = "模板管理", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public AjaxResult refreshCache()
    {
        messageTemplateService.resetTemplateCache();
        return success();
    }
}
